﻿#include "mobilefilewidget.h"

#include <QCheckBox>
#include <QScrollBar>
#include <QDebug>

#include <kysdk/applications/kbreadcrumb.h>
#include "generatetools.h"

using namespace kdk;

const int BUTTON_WIDTH = 20;
const int BUTTON_HEIGHT = 20;

MobileFileWidget::MobileFileWidget(int text, QWidget *parent) : QWidget(parent)
{
    m_text = text;
    qInfo() << "crumb path" << text;
    initUI();
}

MobileFileWidget::~MobileFileWidget()
{
    if (m_loadAnimation != nullptr) {
        m_loadAnimation->deleteLater();
        m_loadAnimation = nullptr;
    }
    if (m_timeLineView != nullptr) {
        m_timeLineView->deleteLater();
        m_timeLineView = nullptr;
    }
}

void MobileFileWidget::showLoadWin()
{
    m_loadAnimation = new LoadAnimation;
    m_loadAnimation->setHighLight();
    m_stackedWin->addWidget(m_loadAnimation);
    m_stackedWin->setCurrentIndex(1);
}

void MobileFileWidget::setMode(QListView::ViewMode mode)
{
    m_mode = mode;
    switch (m_mode) {
    case QListView::ListMode:
        m_modeButton->setIcon(QIcon::fromTheme("view-list-symbolic"));
        m_modeButton->setToolTip(tr("Icon Mode")); // Icon Mode图标模式
        break;
    case QListView::IconMode:
        m_modeButton->setIcon(QIcon::fromTheme("view-grid-symbolic"));
        m_modeButton->setToolTip(tr("List Mode")); // List Mode列表模式
        break;
    default:
        break;
    }
    m_timeLineView->setMode(m_mode);
    Q_EMIT sigModeChange(m_mode);
}

void MobileFileWidget::initUI()
{
    QVBoxLayout *mainVLayout = new QVBoxLayout();
    mainVLayout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(mainVLayout);

    QHBoxLayout *fileListHLayout = new QHBoxLayout();

    m_copyButtonMobileFileWidget = new CopyToComputerButtonWidget(this);
    m_copyButtonMobileFileWidget->hide();

    QPushButton *retreatButton = new QPushButton(this);
    retreatButton->setIcon(QIcon::fromTheme("go-next-rtl-symbolic"));
    retreatButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    retreatButton->setFlat(true);
    retreatButton->setCursor(Qt::PointingHandCursor);

//    QPushButton *forwardButton = new QPushButton(this);
//    forwardButton->setIcon(QIcon::fromTheme("go-next-symbolic"));
//    forwardButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
//    forwardButton->setFlat(true);
//    forwardButton->setCursor(Qt::PointingHandCursor);

    QFont font;
    font.setBold(true);
    //面包屑
    KBreadCrumb *crumb = new KBreadCrumb(this);
    crumb->setFont(font);
    crumb->setFlat(false);
    crumb->addTab(tr("Mobile file list")); // Mobile file list手机文件列表

    QString currentPath;
    int itemIndex;
    if (m_text == ALL_FILE_LIST_TYPE::QQPICTURE) {
        currentPath = tr("QQ") + "/" + tr("Picture"); // QQ QQ    Picture图片
        itemIndex = ALL_FILE_LIST_TYPE::QQPICTURE;
    } else if (m_text == ALL_FILE_LIST_TYPE::QQVIDEO) {
        currentPath = tr("QQ") + "/" + tr("Video"); // QQ QQ     Video视频
        itemIndex = ALL_FILE_LIST_TYPE::QQVIDEO;
    } else if (m_text == ALL_FILE_LIST_TYPE::QQMUSIC) {
        currentPath = tr("QQ") + "/" + tr("Music"); // QQ QQ     Music音乐
        itemIndex = ALL_FILE_LIST_TYPE::QQMUSIC;
    } else if (m_text == ALL_FILE_LIST_TYPE::QQDOCUMENT) {
        currentPath = tr("QQ") + "/" + tr("Document"); // QQ QQ     Document文档
        itemIndex = ALL_FILE_LIST_TYPE::QQDOCUMENT;
    } else if (m_text == ALL_FILE_LIST_TYPE::WECHATPICTURE) {
        currentPath = tr("WeChat") + "/" + tr("Picture"); // WeChat   Picture图片
        itemIndex = ALL_FILE_LIST_TYPE::WECHATPICTURE;
    } else if (m_text == ALL_FILE_LIST_TYPE::WECHATVIDEO) {
        currentPath = tr("WeChat") + "/" + tr("Video"); // WeChat   Video视频
        itemIndex = ALL_FILE_LIST_TYPE::WECHATVIDEO;
    } else if (m_text == ALL_FILE_LIST_TYPE::WECHATMUSIC) {
        currentPath = tr("WeChat") + "/" + tr("Music"); // WeChat   Music音乐
        itemIndex = ALL_FILE_LIST_TYPE::WECHATMUSIC;
    } else if (m_text == ALL_FILE_LIST_TYPE::WECHATDOCUMENT) {
        currentPath = tr("WeChat") + "/" + tr("Document"); // WeChat   Document文档
        itemIndex = ALL_FILE_LIST_TYPE::WECHATDOCUMENT;
    } else if (m_text == ALL_FILE_LIST_TYPE::PICTURE) {
        currentPath = tr("Picture"); // Picture图片
        itemIndex = ALL_FILE_LIST_TYPE::PICTURE;
    } else if (m_text == ALL_FILE_LIST_TYPE::VIDEO) {
        currentPath = tr("Video"); // Video视频
        itemIndex = ALL_FILE_LIST_TYPE::VIDEO;
    } else if (m_text == ALL_FILE_LIST_TYPE::MUSIC) {
        currentPath = tr("Music"); // Music音乐
        itemIndex = ALL_FILE_LIST_TYPE::MUSIC;
    } else if (m_text == ALL_FILE_LIST_TYPE::DOCUMENT) {
        currentPath = tr("Document"); // Document文档
        itemIndex = ALL_FILE_LIST_TYPE::DOCUMENT;
    } else if (m_text == ALL_FILE_LIST_TYPE::WECHAT) {
        currentPath = tr("WeChat"); // WECHAT微信
        itemIndex = ALL_FILE_LIST_TYPE::WECHAT;
    } else if (m_text == ALL_FILE_LIST_TYPE::QQ) {
        currentPath = tr("QQ"); // QQ QQ
        itemIndex = ALL_FILE_LIST_TYPE::QQ;
    }

    if (currentPath.startsWith(tr("WeChat") + "/") || currentPath.startsWith(tr("QQ") + "/")) { // WeChat微信      QQ QQ
        QStringList list = currentPath.split("/");
        for (int i = 0; i < list.size(); i++) {
            crumb->addTab(list.value(i));
        }
        currentPath = list[1];
        crumb->setCurrentIndex(list.size());
    } else {
        crumb->addTab(currentPath);
        crumb->setCurrentIndex(1);
    }

    crumb->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

//    m_searchLine = new SearchEdit(this);
//    m_searchLine->hide();

//    m_searchButton = new QPushButton(this);
//    m_searchButton->setIcon(QIcon::fromTheme("system-search-symbolic"));
//    m_searchButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
//    m_searchButton->setFlat(true);
//    m_searchButton->setCursor(Qt::PointingHandCursor);

//    m_editButton = new QPushButton(this);
//    m_editButton->setIcon(QIcon::fromTheme("document-edit-symbolic"));
//    m_editButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
//    m_editButton->setFlat(true);
//    m_editButton->setCursor(Qt::PointingHandCursor);

//    m_verticalLineFrame = new QFrame(this);
//    m_verticalLineFrame->setFixedHeight(16);
//    m_verticalLineFrame->setFrameShape(QFrame::VLine);
//    m_verticalLineFrame->setFrameShadow(QFrame::Raised);
//    m_verticalLineFrame->setLineWidth(1);

    //模式
    m_modeButton = new QPushButton(this);
    m_modeButton->setIcon(QIcon::fromTheme("view-grid-symbolic"));
    m_modeButton->setToolTip(tr("List Mode")); // List Mode列表模式
    m_modeButton->setCursor(Qt::PointingHandCursor);
    m_modeButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    m_modeButton->setFlat(true);

    //刷新
    QPushButton *refreshButton = new QPushButton(this);
    refreshButton->setIcon(QIcon::fromTheme("view-refresh-symbolic"));
    refreshButton->setToolTip(tr("Refresh")); // Refresh刷新
    refreshButton->setCursor(Qt::PointingHandCursor);
    refreshButton->setFixedSize(BUTTON_WIDTH, BUTTON_HEIGHT);
    refreshButton->setFlat(true);

    QWidget *fileListWidget = new QWidget();
    fileListWidget->setLayout(fileListHLayout);
    fileListWidget->setFixedHeight(35);

    fileListHLayout->addWidget(retreatButton);
//    fileListHLayout->addWidget(forwardButton);
//    fileListHLayout->addSpacing(28);
    fileListHLayout->addWidget(crumb);
    fileListHLayout->addStretch();
//    fileListHLayout->addWidget(m_searchLine);
//    fileListHLayout->addWidget(m_searchButton);
//    fileListHLayout->addSpacing(20);
//    fileListHLayout->addWidget(m_editButton);
//    fileListHLayout->addSpacing(20);
//    fileListHLayout->addWidget(m_verticalLineFrame);
    fileListHLayout->addSpacing(20);
    fileListHLayout->addWidget(m_modeButton);
    fileListHLayout->addSpacing(20);
    fileListHLayout->addWidget(refreshButton);
    fileListHLayout->setContentsMargins(68, 0, 68, 0);

    //列表项
    m_timeLineView = new TimeLineView(itemIndex);

    QWidget *listWidget = new QWidget();

    m_scrollArea = new QScrollArea(this);

    QVBoxLayout *scrollVLayout = new QVBoxLayout();
    QWidget *scrollWin = new QWidget(this);
    scrollWin->setLayout(scrollVLayout);
    scrollVLayout->addWidget(m_scrollArea);

    m_scrollArea->setWidgetResizable(true);

    m_scrollArea->setBackgroundRole(QPalette::Base);
    m_scrollArea->setFrameShape(QFrame::NoFrame);
    m_scrollArea->setWidget(listWidget);

    scrollVLayout->setContentsMargins(68, 0, 68, 30);

    QVBoxLayout *layout = new QVBoxLayout();
    listWidget->setLayout(layout);
    layout->addWidget(m_timeLineView);
    layout->setSpacing(0);
    layout->setMargin(0);

    m_stackedWin = new QStackedWidget(this);
    m_stackedWin->addWidget(scrollWin);
    mainVLayout->addWidget(fileListWidget);
    mainVLayout->addSpacing(24);
    mainVLayout->addWidget(m_stackedWin);

    connect(retreatButton, &QPushButton::clicked, this, [=]() {
        int currentIndex = crumb->currentIndex();
        if (currentIndex > 0) {
            crumb->setCurrentIndex(currentIndex - 1);
        }
    });

//    connect(m_searchLine, &SearchEdit::sigEditFocusIn, this, [=]() {
//        setEditFocus(true);
//    });

//    connect(m_searchLine, &SearchEdit::sigEditFocusOut, this, [=]() {
//        setEditFocus(false);
//    });

//    connect(m_searchButton, &QPushButton::clicked, this, [=]() {
//        setEditFocus(true);
//    });

    connect(refreshButton, &QPushButton::clicked, this, [=]() {
        if (m_text == tr("QQ") + "/" + tr("Picture")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::QQPICTURE);
        } else if (m_text == tr("QQ") + "/" + tr("Video")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::QQVIDEO);
        } else if (m_text == tr("QQ") + "/" + tr("Music")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::QQMUSIC);
        } else if (m_text == tr("QQ") + "/" + tr("Document")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::QQDOCUMENT);
        } else if (m_text == tr("WeChat") + "/" + tr("Picture")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::WECHATPICTURE);
        } else if (m_text == tr("WeChat") + "/" + tr("Video")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::WECHATVIDEO);
        } else if (m_text == tr("WeChat") + "/" + tr("Music")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::WECHATMUSIC);
        } else if (m_text == tr("WeChat") + "/" + tr("Document")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::WECHATDOCUMENT);
        } else if (m_text == tr("Picture")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::PICTURE);
        } else if (m_text == tr("Video")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::VIDEO);
        } else if (m_text == tr("Music")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::MUSIC);
        } else if (m_text == tr("Document")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::DOCUMENT);
        } else if (m_text == tr("WeChat")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::WECHAT);
        } else if (m_text == tr("QQ")) {
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::QQ);
        }
    });
    connect(m_modeButton, &QPushButton::clicked, this, &MobileFileWidget::slotModeButtonClicked);
    connect(crumb, &KBreadCrumb::currentChanged, this, [=](int index) {
        QString text = crumb->tabText(index);
        if (index == 0 && text == tr("Mobile file list")) { // Mobile file list手机文件列表
            slotHideCopyToComputerWidget();
            Q_EMIT sigShowMobileFileListWidget();
            Q_EMIT sigUpdateClicked(ALL_FILE_LIST_TYPE::HOMEPAGE);
            return;
        }

        for (int i = crumb->count() - 1; i > index; i--) {
            crumb->removeTab(i);
        }

        if (index == 1 && (itemIndex >= 0 && itemIndex < 4)) {
            Q_EMIT sigShowMobileFileSecondWidget(ALL_FILE_LIST_TYPE::QQ);
        } else if (index == 1 && (itemIndex >= 4 && itemIndex < 8)) {
            Q_EMIT sigShowMobileFileSecondWidget(ALL_FILE_LIST_TYPE::WECHAT);
        }
        slotHideCopyToComputerWidget();
    });
    connect(m_timeLineView, &TimeLineView::sigItemInitFinish, this, [=]() {
        m_stackedWin->setCurrentIndex(0);
        Q_EMIT sigInitFinish();
        m_loadAnimation->deleteLater();
        m_loadAnimation = nullptr;
    });

    connect(m_timeLineView, &TimeLineView::sigShowCopyButton, this, &MobileFileWidget::slotShowCopyButtonWidget);
    connect(m_timeLineView, &TimeLineView::sigHideCopyToComputerWidget, this,
            &MobileFileWidget::slotHideCopyToComputerWidget);
    connect(m_timeLineView, &TimeLineView::sigSelectDataList, this, [=](const QList<FileInfo> &fileList) {
        m_copyButtonMobileFileWidget->getSelectDataList(fileList);
    });
    connect(m_timeLineView, &TimeLineView::sigSelectAllDataList, this, [=](const QList<FileInfo> &fileList) {
        m_copyButtonMobileFileWidget->getSelectDataList(fileList);
    });
    connect(m_timeLineView, &TimeLineView::sigMenuFilePath, this, [=](QString filePath) {
        Q_EMIT sigMenuFilePath(filePath);
    });
    connect(m_copyButtonMobileFileWidget, &CopyToComputerButtonWidget::sigChooseFilePath, this,
            &MobileFileWidget::sigChooseFilePath);
    connect(this, &MobileFileWidget::sigSetViewHeight, m_timeLineView, [=]() {
        m_timeLineView->setViewHeight();
    });
}

void MobileFileWidget::setEditFocus(bool focus)
{
    if (focus == true) {
        m_searchButton->hide();
        m_editButton->hide();
        m_verticalLineFrame->hide();
        m_searchLine->show();
        m_searchLine->setFocus();
    } else {
        m_searchButton->show();
        m_editButton->show();
        m_verticalLineFrame->show();
        m_searchLine->hide();
        m_searchLine->clearFocus();
    }
}

void MobileFileWidget::slotShowCopyButtonWidget()
{
    if (m_copyButtonMobileFileWidget->isVisible()) {
        moveCopyToComputerWidget();
    } else {
        moveCopyToComputerWidget();
        m_copyButtonMobileFileWidget->show();
        m_copyButtonMobileFileWidget->raise();
    }
}

void MobileFileWidget::slotHideCopyToComputerWidget()
{
    if (m_copyButtonMobileFileWidget->isVisible()) {
        m_copyButtonMobileFileWidget->hide();
    }
}

void MobileFileWidget::moveCopyToComputerWidget()
{
    m_copyButtonMobileFileWidget->move(
        QPoint(this->width() / 2 - m_copyButtonMobileFileWidget->width() / 2,
               this->height() - 16 - m_copyButtonMobileFileWidget->height()));
}

void MobileFileWidget::setThumbanil(QString fileName)
{
    m_timeLineView->setThumbnail(fileName);
}

void MobileFileWidget::slotModeButtonClicked()
{
    int current = 0;
    // 切换视图埋点
    GenerateTools::buriedPoint(kabase::BuriedPoint::PT::KylinMobileAssistantSwitchView);
    switch (m_mode) {
    case QListView::IconMode:
        m_mode = QListView::ListMode;
        m_modeButton->setIcon(QIcon::fromTheme("view-list-symbolic"));
        m_modeButton->setToolTip(tr("Icon Mode")); // Icon Mode图标模式
        current = 1;
        break;
    case QListView::ListMode:
        m_mode = QListView::IconMode;
        m_modeButton->setIcon(QIcon::fromTheme("view-grid-symbolic"));
        m_modeButton->setToolTip(tr("List Mode")); // List Mode列表模式
        current = 0;
        break;
    default:
        break;
    }
    m_timeLineView->setMode(m_mode);
    m_timeLineView->setCurrentViewMode(current);
    Q_EMIT sigModeChange(m_mode);
}

void MobileFileWidget::setTimeLineData(const QList<FileInfo> &listStruct)
{
    m_timeLineView->setTimeLineData(listStruct);
    m_timeLineView->setPictureTimeLineDateWidget();
}

void MobileFileWidget::setMusicData(const QList<FileInfo> &listStruct)
{
    m_timeLineView->setData(listStruct);
    m_timeLineView->setMusicDateWidget();
}

void MobileFileWidget::setDocumenData(const QList<FileInfo> &listStruct)
{
    m_timeLineView->setData(listStruct);
    m_timeLineView->setDocumentDateWidget();
}

void MobileFileWidget::setViewHeight()
{
    Q_EMIT sigSetViewHeight();
    if (m_copyButtonMobileFileWidget) {
        moveCopyToComputerWidget();
    }
}

void MobileFileWidget::setTheme(PublicAttributes::Theme theme)
{
    if (theme == PublicAttributes::Theme::Light) {
        QPalette scrollPalette = m_scrollArea->palette();
        scrollPalette.setColor(QPalette::Base, QColor("#FFFEFD"));
        m_scrollArea->setPalette(scrollPalette);
        if (m_timeLineView != nullptr) {
            m_timeLineView->setTheme(theme);
        }
    } else if (theme == PublicAttributes::Theme::Dark) {
        QPalette scrollPalette = m_scrollArea->palette();
        scrollPalette.setColor(QPalette::Base, QColor("#1D1D1D"));
        m_scrollArea->setPalette(scrollPalette);
        if (m_timeLineView != nullptr) {
            m_timeLineView->setTheme(theme);
        }
    }
}
